package com.oil.petro.eshop.service;

import com.oil.petro.eshop.bean.BasicBean;
import com.oil.petro.eshop.bean.ProductBean;
import com.oil.petro.eshop.dao.CategoryDAO;
import com.oil.petro.eshop.dao.ManufacturerDAO;
import com.oil.petro.eshop.dao.PersonDAO;
import com.oil.petro.eshop.dao.ProductDAO;
import com.oil.petro.eshop.entity.Category;
import com.oil.petro.eshop.entity.Product;
import com.oil.petro.eshop.utils.OrderStatusUtil;
import com.oil.petro.eshop.utils.Price;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional()/**/
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductDAO productDAO;

    @Autowired
    CategoryDAO categoryDAO;

    @Autowired
    ManufacturerDAO manufacturerDAO;

    @Transactional
    @Override
    public Product getProduct(int id) {
        return productDAO.get(id);
    }

    @Transactional
    @Override
    public void deleteProduct(Product product) {
        productDAO.delete(product);
    }

    @Override
    public void deleteProduct(int productId) {

        productDAO.delete(productId);
    }

    @Transactional
    @Override
    public int saveProduct(Product product) {
        return productDAO.save(product);
    }

    @Transactional
    @Override
    public void updateProduct(Product product) {
        productDAO.update(product);
    }

    @Override
    public List<Category> getCategoryList() {
        return categoryDAO.getList();
    }

    @Override
    public List<Product> getProductList(int amount, int page) {
        return productDAO.getList(amount, page);
    }

    @Override
    public List<Product> getProductListForCategory(int categoryId) {
        return productDAO.getProductListForCategory(categoryId);
    }

    public List<Product> searchProductList(BasicBean basicBean, int amount, int pageNumber) {

        //basicBean
        int categoryId = -1;
        int manufacturerId = -1;
        boolean orderFlag = false;
        if (!basicBean.getCategory().equals("none")) {
            categoryId = categoryDAO.getId(basicBean.getCategory());
        }
        if (!basicBean.getManufacturer().equals("none")) {
            manufacturerId = manufacturerDAO.getId(basicBean.getManufacturer());
        }
        if (Price.PICKER_PRICE[1].equals(basicBean.getCost())) {
            orderFlag = true;

            System.out.println("test");
        }

        return productDAO.searchProductList(categoryId, manufacturerId, orderFlag, amount, pageNumber);
    }

    public boolean isNextProductListPage(int amount, int page) {

        List<Product> productList = getProductList(amount, page - 1);

        if (productList.isEmpty()) {
            return false;
        } else {
            return true;
        }

    }

    public boolean isNextSearchProductListPage(BasicBean basicBean, int amount, int page) {

        List<Product> productList = searchProductList(basicBean, amount, page - 1);

        if (productList.isEmpty()) {
            return false;
        } else {
            return true;
        }

    }

    @Override
    public boolean updateProduct(ProductBean productBean, Product product) {
        int amount = Integer.parseInt(productBean.getOrderedQuantity());
        if (product.getAmount() - amount >= 0) {

            product.setAmount(product.getAmount() - amount);

            if (product.getAmount() - amount == 0) {

                product.setOrderStatus(OrderStatusUtil.UNAVAILABLE);

            } else {

                product.setOrderStatus(OrderStatusUtil.IN_CART);
            }

            productDAO.update(product);

            return true;

        }

        return false;

    }

    @Override
    public List<Product> getProductListForEdit() {

        return productDAO.getList(OrderStatusUtil.IN_CART);

    }

    @Override
    public List<Product> getProductListForNewAndUnavailable() {
        List<Product> list = new ArrayList<>();
        list.addAll(productDAO.getList(OrderStatusUtil.NEW));
        list.addAll(productDAO.getList(OrderStatusUtil.UNAVAILABLE));

        return list;

    }

    @Override
    public List<Product> getProductList(int status) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
